package com.hudilao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hudilao.common.R;
import com.hudilao.entity.Employee;
import com.hudilao.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    //管理端：员工登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //密码md5加密后在存储
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //根据username查询
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        // 如果没有查询到则返回登录失败结果
        if (emp == null) {

            log.error("用户不存在，登录失败");
            return R.error("用户不存在，登录失败");
        }

        // 比对密码
        if (!emp.getPassword().equals(password)) {

            log.error("密码错误，登录失败");
            return R.error("密码错误，登录失败");
        }

        // 查看员工状态，如果为已禁用，则返回已禁用结果
        if (emp.getStatus() == 0) {

            log.error("账号已禁用");
            return R.error("账号已禁用");
        }

        request.getSession().setAttribute("employee", emp.getId());

        //要返回employee的role，决定有哪些功能
        log.info(emp.getRole().toString());
        return R.success(emp);
    }


    //管理端：员工注册
    @PostMapping("/register")
    public R<Employee> register(@RequestBody Employee employee) {

        //员工注册
        return employeeService.registerEmployee(employee);
    }


    //管理端：退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {

        //退出登录
        request.getSession().removeAttribute("employee");
        return R.success("退出登录成功");
    }


    //管理端：新增员工
    @PostMapping("/save")
    public R<String> save(@RequestBody Employee employee) {

        //新增员工
        return employeeService.saveEmployee(employee);
    }


    //管理端：员工分页查询
    @GetMapping("/employeeList")
    public R<Page> page(int page, int pageSize, String username) {

        //员工分页查询（参数page，pageSize，username）
        return employeeService.pageEmployee(page, pageSize, username);
    }


    //管理端：改变员工状态
    @PutMapping("/status")
    public R<String> updateStatus(@RequestBody Employee employee) {

        //改变员工状态
        return employeeService.updateEmployeeStatus(employee);
    }


    //管理端：修改员工信息
    @PutMapping("/update")
    public R<String> update(@RequestBody Employee employee) {

        //修改员工信息
        return employeeService.updateEmployee(employee);
    }


    //管理端：根据id查询员工信息
    @GetMapping("/edit/{id}")
    public R<Employee> getById(@PathVariable Long id) {

        //根据id查询员工信息
        return employeeService.getEmployeeById(id);
    }


    //管理端：根据id删除员工
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {

        //删除员工
        return employeeService.deleteEmployee(id);
    }
}
